package com.ruoyi.asset.util;

import com.ruoyi.asset.domain.TAsset;
import com.ruoyi.asset.domain.dto.AssetDTO;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资产转换工具类
 * 用于TAsset和AssetDTO之间的转换
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
public class AssetConverter 
{
    /**
     * TAsset转换为AssetDTO
     */
    public static AssetDTO toDTO(TAsset asset) 
    {
        if (asset == null) {
            return null;
        }
        
        AssetDTO dto = new AssetDTO();
        dto.setAssetNo(asset.getAssetNo());
        dto.setDeviceNo(asset.getDeviceNo());
        dto.setAssetName(asset.getName()); // 映射 name -> assetName
        dto.setCategoryId(asset.getCategoryId());
        dto.setEquipmentTypeCode(asset.getEquipmentTypeCode());
        dto.setModelNo(asset.getModelNo());
        dto.setBrand(asset.getBrand());
        dto.setLocation(asset.getLocation());
        dto.setDeptId(asset.getDeptId());
        // 将deptId作为deptName显示
        dto.setDeptName(asset.getDeptId());
        dto.setSupplierName(asset.getSupplierName());
        dto.setManufacturer(asset.getManufacturer());
        dto.setPurchaseDate(asset.getPurchaseDate());
        dto.setCommissioningDate(asset.getCommissioningDate());
        dto.setOperatorName(asset.getOperatorName());
        dto.setImageUrls(asset.getImageUrls());
        dto.setStatusCode(asset.getStatusCode());
        dto.setIsDelete(asset.getIsDelete());
        dto.setCreatedAt(asset.getCreatedAt());
        dto.setUpdatedAt(asset.getUpdatedAt());
        dto.setRemark(asset.getRemark());
        return dto;
    }

    /**
     * AssetDTO转换为TAsset
     */
    public static TAsset toEntity(AssetDTO dto) 
    {
        if (dto == null) {
            return null;
        }
        
        TAsset asset = new TAsset();
        asset.setAssetNo(dto.getAssetNo());
        asset.setDeviceNo(dto.getDeviceNo());
        asset.setName(dto.getAssetName()); // 映射 assetName -> name
        asset.setCategoryId(dto.getCategoryId());
        asset.setEquipmentTypeCode(dto.getEquipmentTypeCode());
        asset.setModelNo(dto.getModelNo());
        asset.setBrand(dto.getBrand());
        asset.setLocation(dto.getLocation());
        // 优先使用deptName，如果没有则使用deptId
        String deptValue = dto.getDeptName() != null ? dto.getDeptName() : dto.getDeptId();
        asset.setDeptId(deptValue);
        asset.setSupplierName(dto.getSupplierName());
        asset.setManufacturer(dto.getManufacturer());
        asset.setPurchaseDate(dto.getPurchaseDate());
        asset.setCommissioningDate(dto.getCommissioningDate());
        asset.setOperatorName(dto.getOperatorName());
        asset.setImageUrls(dto.getImageUrls());
        asset.setStatusCode(dto.getStatusCode());
        asset.setIsDelete(dto.getIsDelete());
        asset.setCreatedAt(dto.getCreatedAt());
        asset.setUpdatedAt(dto.getUpdatedAt());
        asset.setRemark(dto.getRemark());
        return asset;
    }

    /**
     * TAsset列表转换为AssetDTO列表
     */
    public static List<AssetDTO> toDTOList(List<TAsset> assets) 
    {
        if (assets == null) {
            return null;
        }
        
        return assets.stream()
                .map(AssetConverter::toDTO)
                .collect(Collectors.toList());
    }

    /**
     * AssetDTO列表转换为TAsset列表
     */
    public static List<TAsset> toEntityList(List<AssetDTO> dtos) 
    {
        if (dtos == null) {
            return null;
        }
        
        return dtos.stream()
                .map(AssetConverter::toEntity)
                .collect(Collectors.toList());
    }
}
